local m = require('utils.vim')
local hl = vim.api.nvim_set_hl
local c = require('utils.color')

-- Don't auto commenting new lines
m.autocmd('BufEnter', {
    pattern = '',
    command = 'set fo-=c fo-=r fo-=o'
})

-- clear jumlist
m.autocmd('VimEnter', {
    pattern = '',
    command = 'clearjumps'
})

-- 清除 on save
m.autocmd('BufWritePre', {
    pattern = '',
    callback = function()
        local lines = vim.api.nvim_buf_line_count(0)
        if lines > 2000 then
            return
        end -- big file

        m.cmd("silent! %s/\r//g") -- clear ^M
        m.cmd("silent! %s/\\s\\+$//e") -- clear whitespace
    end,
})

-- start command for terminal
m.autocmd("TermOpen", {
    pattern = "*",
    callback = function()
        local bufnr = vim.api.nvim_get_current_buf()
        local buf_name = vim.api.nvim_buf_get_name(bufnr)
        --require('notify')(buf_name)
        if string.match(buf_name, "bin") then
            -- not for git commit terminal
            vim.cmd("startinsert")

            local command = "source ~/fish_start.fish;c"
            vim.fn.chansend(vim.b.terminal_job_id, command .. "\r")
        end
    end,
})

-- set filetype to lua when open file without extension(html->lua)
vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
    pattern = "*",
    callback = function()
        local filename = vim.fn.expand("%:t")
        local ext = vim.fn.expand("%:e")

        if ext == "html" then
            vim.bo.filetype = "lua"
        elseif ext == "json" then
            vim.bo.filetype = "lua"
        elseif ext == "yml" then
            vim.bo.filetype = "lua"
        elseif ext == "htm" then
            vim.bo.filetype = "lua"
        elseif filename ~= "" and ext == "" then
            vim.bo.filetype = "lua"
        end
    end,
})

-- EOP
